package com.wwy.configuraion;

import com.wwy.api.RedisSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisSentinelConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Configuration
public class RedisSourceConfig {

    @Value("#{'${redis.sentinel.nodes}'.split(',')}")
    private List<String> nodes;

    @Value("${redis.host}")
    private String host;

    @Value("${redis.port}")
    private String port;

    @Value("${redis.db}")
    private String db;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * jedis配置
     * @return
     */
    @Bean
    public JedisPoolConfig poolConfig() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(8);
        poolConfig.setMaxTotal(8);
        poolConfig.setBlockWhenExhausted(true);
        poolConfig.setMaxWaitMillis(30000);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        poolConfig.setTimeBetweenEvictionRunsMillis(1200000);
        poolConfig.setMinEvictableIdleTimeMillis(1800000);
        return poolConfig;
    }

    /**
     * 哨兵配置
     * @return
     */
    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration(){
        RedisSentinelConfiguration sentinel = new RedisSentinelConfiguration();
        sentinel.setMaster("mymaster");
        Set<RedisNode> sentinels = new HashSet<>();
        nodes.forEach(li ->{
            li.toString().split(":");
            // 多个哨兵
            sentinels.add(new RedisNode(li.split(":")[0],Integer.parseInt(li.split(":")[1])));
        });

        sentinel.setSentinels(sentinels);

        return sentinel;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
        JedisConnectionFactory jedisFactory = new JedisConnectionFactory(redisSentinelConfiguration(),jedisPoolConfig);// 配置哨兵
        //JedisConnectionFactory jedisFactory = new JedisConnectionFactory(); //
        jedisFactory.setPoolConfig(jedisPoolConfig);
        //jedisFactory.setHostName(host); // 配置哨兵无需设置
        //jedisFactory.setPort(Integer.parseInt(port)); // 配置哨兵无需设置
        //jedisFactory.setPassword("redis@20191210"); // 无密码无需设置
        jedisFactory.setUsePool(true);
        jedisFactory.setDatabase(Integer.parseInt(db));
        return jedisFactory;
    }



    @Bean
    public RedisTemplate<String, Object> stringSerializerRedisTemplate() {
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        return redisTemplate;
    }
}
